import pygtk
pygtk.require('2.0')
import gtk
# import os, sy
import os, sys

from mokocard.util.sqlutil import SqlUtil
from mokocard.util.logutil import LogUtil
from mokocard.gui.modaldialog import ModalDialog

class MainWindow:
    
    sqlUtil = None
    
    def delete_event(self, widget, event, data=None):
        # If you return FALSE in the "delete_event" signal handler,
        # GTK will emit the "destroy" signal. Returning TRUE means
        # you don't want the window to be destroyed.
        # This is useful for popping up 'are you sure you want to quit?'
        # type dialogs.
        LogUtil.writeLog("delete event occurred on modify window")
        
        # Change FALSE to TRUE and the main window will not be destroyed with a "delete_event".

        return False

    def destroy(self, widget, data=None):
        
        # close log file
        LogUtil.writeLog("destroy signal occurred on modify window")
        
    def updateRow(self, widget, id):
        
        if self.entryQuestion.get_text() == None or self.entryAnswer.get_text() == None or self.entryQuestion.get_text().strip() == "" or self.entryAnswer.get_text().strip() == "":
            modal = ModalDialog("No question or answer")
            return None

        ret = self.sqlUtil.updateCard(self.entryQuestion.get_text() , self.entryAnswer.get_text(), id)
        
        if ret == True:
            modal = ModalDialog("Record Updated")
            self.entryAnswer.set_text("")
            self.entryQuestion.set_text("")
        else:
            modal = ModalDialog("Record Not Updated")
        
    def buildLayout(self, question, answer, id):
        
        # build table
        tableUpdateData = gtk.Table(4,1,False)
        tableUpdateData.set_border_width(10)
        tableUpdateData.show()
        
        # parameter homogeneous, spacing
        hEntryQuestion = gtk.HBox(False,10)
        hEntryQuestion.show()
        
        self.entryQuestion = gtk.Entry()
        self.entryQuestion.set_text(question)
        self.entryQuestion.show()
        
        lblQuestion = gtk.Label("Question: ")
        lblQuestion.show()
        
        # parameters child, expand, fill, padding
        hEntryQuestion.pack_start(lblQuestion, False, False, 10)
        hEntryQuestion.pack_start(self.entryQuestion, True, True, 10)
        
        # parameter homogeneous, spacing
        hEntryAnswer = gtk.HBox(False,10)
        hEntryAnswer.show()
        
        self.entryAnswer = gtk.Entry()
        self.entryAnswer.set_text(answer)
        self.entryAnswer.show()
        
        lblAnswer = gtk.Label("Answer: ")
        lblAnswer.show()
        
        hEntryAnswer.pack_start(lblAnswer, False, False, 10)
        hEntryAnswer.pack_start(self.entryAnswer, True, True, 10)
        
        btUpdateRow = gtk.Button("Update")
        btUpdateRow.connect("clicked", self.updateRow, id)
        btUpdateRow.show()
        
        # end insert data zone
        lblUpdateRow = gtk.Label("Insert Row ")
        lblUpdateRow.show()
        
        tableUpdateData.attach(lblUpdateRow,0,1,0,1, ypadding=10)
        tableUpdateData.attach(hEntryQuestion,0,1,1,2, ypadding=10)
        tableUpdateData.attach(hEntryAnswer,0,1,2,3, ypadding=10)
        tableUpdateData.attach(btUpdateRow,0,1,3,4, ypadding=10)
        
        self.pack_start(tableInsertData, False, False, 10)
        
    def __init__(self, question, answer, id, sqlUtil):        
        
        # create a new window
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    
        # When the window is given the "delete_event" signal (this is given by the window manager, usually by the "close" option, or on the titlebar), we ask it to call the delete_event () function as defined above. 
        # The data passed to the callback function is NULL and is ignored in the callback function.
        self.window.connect("delete_event", self.delete_event)
    
        # Here we connect the "destroy" event to a signal handler.  
        # This event occurs when we call gtk_widget_destroy() on the window, or if we return FALSE in the "delete_event" callback.
        self.window.connect("destroy", self.destroy)
    
        # Sets the border width of the window.
        self.window.set_border_width(10)
        
        # title
        self.window.set_title("Modify record")
        
        # size
        self.window.set_default_size(480,640)

        self.sqlUtil = sqlUtil
        self.buildLayout(question, answer, id)

        
        self.window.show()
        
    def main(self):
        
        # All PyGTK applications must have a gtk.main(). Control ends here and waits for an event to occur (like a key press or mouse event).
        gtk.main()
